package com.example.hcbmsm.service.impl;

import com.auth0.jwt.interfaces.Claim;
import com.example.hcbmsm.domain.Ai;
import com.example.hcbmsm.domain.Doc;
import com.example.hcbmsm.domain.DocExample;
import com.example.hcbmsm.domain.Pat;
import com.example.hcbmsm.mapper.AiMapper;
import com.example.hcbmsm.mapper.DocMapper;
import com.example.hcbmsm.service.AiService;
import com.example.hcbmsm.service.DocService;
import com.example.hcbmsm.util.HCBMSMResult;
import com.example.hcbmsm.util.JavaWebToken;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.*;

/**
 * @author Xinghm
 * @version 1.0
 * @since 2024/10/29 22:52
 */
@Service
public class DocServiceImpl implements DocService {
    @Autowired
    private DocMapper docMapper;
    @Autowired
    private AiService aiService;
    @Override
    public Map findDocByDeptId(Long deptId) {
        DocExample docExample = new DocExample();
        docExample.createCriteria().andDeptIdEqualTo(deptId);
        return HCBMSMResult.SUCCESS(docMapper.selectByExample(docExample));
    }

    @Override
    public Map getUser(String token) {
        Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
        Long docId = new Long(tokenInfo.get("docId").asString());
        Doc doc = docMapper.selectByPrimaryKey(docId);
        doc.setDocPassword("");
        return HCBMSMResult.SUCCESS(doc);
    }

    @Override
    public Map list(String docName, int page, int limit) {
        Integer total = docMapper.selectDocList(docName).size();
        PageHelper.startPage(page, limit);
        List<Doc> list = docMapper.selectDocList(docName);
        for (Doc doc : list) {
            doc.setDocPassword("");
        }
        Map map = new HashMap<>();
        map.put("total", total);
        map.put("data", list);
        return HCBMSMResult.SUCCESS(map);
    }

    @Override
    public Map add(Doc doc) {
        DocExample docExample = new DocExample();
        docExample.createCriteria().andDocNameEqualTo(doc.getDocName());
        List<Doc> docs = docMapper.selectByExample(docExample);
        if (!docs.isEmpty() && !Objects.equals(docs.get(0).getDocId(), doc.getDocId())) {
            return HCBMSMResult.WARNING(400, "用户名已存在");
        }
        if (doc.getDocPassword() != null) {
            doc.setDocPassword(DigestUtils.md5DigestAsHex(doc.getDocPassword().getBytes()));
        }
        if (docMapper.insertSelective(doc) == 1) {
            Ai ai = new Ai();
            docExample = new DocExample();
            docExample.createCriteria().andDocNameEqualTo(doc.getDocName());
            doc = docMapper.selectByExample(docExample).get(0);
            ai.setDocId(doc.getDocId());
            aiService.add(ai);
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map renew(Doc doc) {
        DocExample docExample = new DocExample();
        docExample.createCriteria().andDocNameEqualTo(doc.getDocName());
        List<Doc> docs = docMapper.selectByExample(docExample);
        if (!docs.isEmpty() && !Objects.equals(docs.get(0).getDocId(), doc.getDocId())) {
            return HCBMSMResult.WARNING(400, "用户名已存在");
        }
        if (doc.getDocPassword() != null) {
            doc.setDocPassword(DigestUtils.md5DigestAsHex(doc.getDocPassword().getBytes()));
        }
        doc.setModifyTime(new Date());
        if (docMapper.updateByPrimaryKeySelective(doc) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map remove(Long docId) {
        if (docMapper.deleteByPrimaryKey(docId) == 1) {
            aiService.remove(docId);
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }
}
